package visiopuce.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import visiopuce.objets.FeuillePuce;
import visiopuce.objets.Prelevement;
import visiopuce.service.FeuillePuceServiceImpl;
import visiopuce.ui.providers.FeuillePuceLabelProvider;

public class IdFeuillePuceDialog extends Dialog {

	private ComboViewer comboFeuillePuce;
	private Button btnOk;
	private Button btnAnnuler;
	String titre;
	private FeuillePuce feuillePuce;

	public IdFeuillePuceDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Choix de la feuille de travail");
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(1, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Label lblQuestion = new Label(container, SWT.NONE);
		lblQuestion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblQuestion.setText("Quelle feuille de travail voulez-vous ouvrir?");

		comboFeuillePuce = new ComboViewer(container, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboFeuillePuce.setContentProvider(ArrayContentProvider.getInstance());
		comboFeuillePuce.setLabelProvider(new FeuillePuceLabelProvider());
		comboFeuillePuce.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboFeuillePuce.setInput(FeuillePuceServiceImpl.getInstance().getFeuillePuce());

		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Ok", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(350, 200);
	}

	@Override
	protected void okPressed() {
		ISelection selectionDerive = comboFeuillePuce.getSelection();
		if (!selectionDerive.isEmpty()) {
			IStructuredSelection selection = (IStructuredSelection) selectionDerive;
			feuillePuce = (FeuillePuce) selection.getFirstElement();
		}
		super.okPressed();
	}

	public FeuillePuce getFeuillePuce() {
		return feuillePuce;
	}

}